﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LastResult.aspx.cs" Inherits="CRMWeb.Admin.LastResult" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Last Result</title>
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/Scripts/common.js"></script>
</head>
<body>
    <table id="dgLastResult" title="Last Result"  style="width: 600%; height: 370px;overflow-y:auto;" toolbar="#toolbarLastResult"
        rownumbers="true" fitcolumns="true" singleselect="true" remotesort="false">
        <thead>
            <tr>
                <th field="OrderNo" width="100px" sortable="true">OrderNo</th>
                <th field="ResultDescription" width="500px" sortable="true">Result Description</th>
                <th  field="ResultID"  width="100px" >ResultID</th>
               <%--  style="display: none"--%>
            </tr>
        </thead>
    </table>
    <div id="toolbarLastResult">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newLastResult()">New</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editLastResult()">Edit</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteLastResult()">Delete</a>
         <a href="javascript:void(0)" id="A1" class="easyui-linkbutton" iconcls="icon-upArrow" plain="true" onclick="RowSwap(-1)">Up</a>
         <a href="javascript:void(0)"  class="easyui-linkbutton" iconcls="icon-downArrow" plain="true" onclick="RowSwap(1)">Down</a>
         <a href="javascript:void(0)"  class="easyui-linkbutton" plain="true" iconcls="icon-ok" onclick="ConfirmOrderChange()">Confirm</a>

        
    </div>
    <div id="dlgLastResult" class="easyui-dialog" style="width: 350px; height: 220px; padding: 10px 20px"
        closed="true" buttons="#dlg-buttons">
        <div class="ftitle">Last Result Information</div>
        <form id="fmLastResult" method="post" novalidate>
            <div class="fitem">
                <label style="width: 130px;">Result Description:</label>
                <input id="txtResultDescription" style="width: 292px;" class="easyui-validatebox" required="true" />
                <input id="hdnResultID" value="" style="display: none" />
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgLastResult').dialog('close')" style="float: left">Cancel</a>
        <a href="javascript:void(0)"  class="easyui-linkbutton" iconcls="icon-ok" onclick="saveLastResult()">Save</a>
    </div>
    <script type="text/javascript">
        var url;
        function newLastResult() {
            $('#dlgLastResult').dialog('open').dialog('setTitle', 'New Last Result');
            $('#fmLastResult').form('clear');
            $('#txtResultDescription').val('');
            $('#hdnResultID').val('');
            url = '/WebServices/LastResult/LastResultAdd';
        }
        window.onresize = function () {
            $('#dgLastResult').datagrid('resize', {
                //width: window.innerWidth,
                //height: window.innerHeight
            });
        }
        function editLastResult() {
            var row = $('#dgLastResult').datagrid('getSelected');
            if (row) {
                $('#dlgLastResult').dialog('open').dialog('setTitle', 'Edit Last Result');
                $('#fmLastResult').form('load', row);
                $('#txtResultDescription').val(row.ResultDescription);
                $('#hdnResultID').val(row.ResultID);
                url = '/WebServices/LastResult/LastResultEdit?ResultID=' + row.ResultID;
            }
        }
        function deleteLastResult() {
            var row = $('#dgLastResult').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/LastResult.ashx',
                    type: 'POST',
                    data: {
                        method: 'LastResultDelete',
                        args: { ResultID: row.ResultID }
                    },
                    success: function (data) {
                        $('#dgLastResult').datagrid('reload');
                    }
                });
            }
        }
        function saveLastResult() {
            var strResultDescription = $('#txtResultDescription').val();
            var ResultID = $('#hdnResultID').val();

            if (ResultID != '') {
                if (strResultDescription != '') {
                    $.ajax({
                        url: '/WebServices/LastResult.ashx',
                        type: 'POST',
                        data: {
                            method: 'LastResultEdit',
                            args: { ResultID: ResultID, ResultDescription: strResultDescription }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgLastResult').dialog('close');
                                $('#dgLastResult').datagrid('reload');
                            }
                            else {
                                alert('ResultDescription already exist');
                            }
                        }
                    });
                }
            }
            else {
                if (strResultDescription != '') {
                    $.ajax({
                        url: '/WebServices/LastResult.ashx',
                        type: 'POST',
                        data: {
                            method: 'LastResultAdd',
                            args: { ResultDescription: strResultDescription }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgLastResult').dialog('close');
                                $('#dgLastResult').datagrid('reload');
                            }
                            else {
                                alert('ResultDescription already exist.');
                            }
                        }
                    });
                }
            }
        }

        $(document).ready(function () {
            $('#dgLastResult').datagrid({
                
                url:'/WebServices/LastResult.ashx?method=LastResultSelect',
                onDblClickRow: function (index, rowdata) {
                    editLastResult();
                }
            });
        });

        //Function swap rows:
        function RowSwap(step) {
            var dg = $('#dgLastResult');
            var rows = dg.datagrid('getRows');
            var rowsCount = dg.datagrid('getRows').length;
            var selectedRow = dg.datagrid('getSelected');

            if (selectedRow) {
                var selectedRowIndex = $("#dgLastResult").datagrid("getRowIndex", selectedRow);
                var selectedRowResultDescription = selectedRow.ResultDescription;
                var selectedRowResultID = selectedRow.ResultID;
                var destinationRowIndex = selectedRowIndex + step;

                if (destinationRowIndex >= 0 && destinationRowIndex < rowsCount) {
                    var destinationRowDescription = rows[destinationRowIndex].ResultDescription;
                    var destinationRowResultID = rows[destinationRowIndex].ResultID;
                    //Update row description after swap
                    updateResultRow(destinationRowIndex, selectedRowResultDescription, selectedRowResultID);
                    updateResultRow(selectedRowIndex, destinationRowDescription, destinationRowResultID);
                    //Set destination row as selected row
                    dg.datagrid('selectRow', destinationRowIndex);
                } else if (step == -1) {
                    alert("Can NOT move the first row up.");
                } else {
                    alert("Can NOT move the last row down");
                }
            } else {
                alert("Please select one row.");
            }
        }

        function updateResultRow(rowIndex, resultDescription, resultId) {
            $('#dgLastResult').datagrid('updateRow', {
                index: rowIndex,
                row: {
                    ResultDescription: resultDescription,
                    ResultID: resultId
                }
            });
        }

        function ConfirmOrderChange() {
            var dg = $('#dgLastResult');
            var rows = dg.datagrid('getRows');
            var selectedRow = dg.datagrid('getSelected');
           // var isF
            if (selectedRow) {
                $.each(rows, function (i, row) {
                    $.ajax({
                        url: '/WebServices/LastResult.ashx',
                        type: 'POST',
                        data: {
                            method: 'ReorderLastResult',
                            args: {
                                resultId: rows[i].ResultID,
                                orderNo: rows[i].OrderNo
                            }
                        },
                        success: function (data) {
                            if (data == 1) {
                                //success for saving
                            }
                            else {
                                alert('Fail for saving.');
                            }
                        }
                    });

                });
                //Reload data
                //dg.datagrid({
                //    url: '/WebServices/LastResult.ashx?method=LastResultSelect'
                //});
              //  dg.datagrid('reload');
            }
            else {
                alert("nochange");
            }
           // dg.datagrid('reload');
        }
       
    </script>
</body>
</html>
